﻿<Window 
    x:Class="SearchForWorkitemsChanges.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:My="clr-namespace:SearchForWorkitemsChanges"
    Title="Search for Team Foundation Server workitems changes" Height="600" Width="auto" Icon="/Theme/Images/face-devilish.png" >
    <Window.Resources>
        <!-- Icon face-devilish.png origin : http://openiconlibrary.sourceforge.net/ -->
        <My:VmMainWindow x:Key="VM"></My:VmMainWindow>
        <My:IEnumerableToBoolConverter x:Key="ItemsExistsToBoolEnabledConverter"></My:IEnumerableToBoolConverter>
        
        <DataTemplate x:Key="ChangesetDataTemplate">
            <TextBlock  >
                <TextBlock.Text>
                    <MultiBinding StringFormat="C {0} ({1:yyyy/MM/dd hh:mm})">
                        <Binding Path="ChangesetId" />
                        <Binding Path="CreationDate" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>

        <Style TargetType="Button">
            <Setter Property="Margin" Value="5"></Setter>
        </Style>

        <Style TargetType="ComboBox">
            <Setter Property="Margin" Value="5"></Setter>
        </Style>


        <Style TargetType="TextBlock">
            <Setter Property="Margin">
                <Setter.Value>
                    <Thickness Top="5" Bottom="5" Left="5" Right="5" />
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="GroupBox">
            <Setter Property="Margin">
                <Setter.Value>
                    <Thickness Top="5" Bottom="5" Left="5" Right="5" />
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush">
                <Setter.Value>
                    <SolidColorBrush Color="Gray" />
                </Setter.Value>
            </Setter>
        </Style>

        <HierarchicalDataTemplate DataType="{x:Type My:BranchLazyLoader}" x:Key="BranchDataTemplate"  ItemsSource="{Binding Children}" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Image Grid.Column="0" Source="/Theme/Images/IconBranch.png" />
                <TextBlock Grid.Column="1" Text="{Binding Path=Name}" />
            </Grid>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type My:FolderLazyLoader}" x:Key="FolderDataTemplate" ItemsSource="{Binding Children}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Image Grid.Column="0" Source="/Theme/Images/IconFolder.png" />
                <TextBlock Grid.Column="1" Text="{Binding Path=Name}" />
            </Grid>
        </HierarchicalDataTemplate>

        <My:BranchOrFolderTemplateSelector x:Key="TrvServerSystemItemsTemplateSelector" BranchDataTemplate="{StaticResource BranchDataTemplate}" FolderDataTemplate="{StaticResource FolderDataTemplate}"></My:BranchOrFolderTemplateSelector>

        
        
    </Window.Resources>

    <Grid DataContext="{Binding Source={StaticResource VM}}">
        <Grid.Resources>
            <CollectionViewSource x:Key="ChangesetsFromLimit" Source="{Binding ChangeSets}">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" PropertyName="ChangesetId" Direction="Ascending"></scm:SortDescription>
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>

            <CollectionViewSource x:Key="ChangesetsToLimit" Source="{Binding ChangeSets}">
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" PropertyName="ChangesetId" Direction="Descending"></scm:SortDescription>
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="290" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>

            <GroupBox Header="2. Select a branch to work with">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <TextBlock>
                        <TextBlock.Text>
                            <Binding Path="SelectedFolder.Name" StringFormat="Current : {0}" Mode="OneWay" FallbackValue="(no selection)" TargetNullValue="(no selection)"></Binding>
                        </TextBlock.Text>
                    </TextBlock>
                    <TreeView Grid.Row="1" ItemTemplateSelector="{StaticResource TrvServerSystemItemsTemplateSelector}"  ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Auto" Grid.Column="0" ItemsSource="{Binding Folders, Mode=OneWay}" SelectedItemChanged="TreeView_SelectedItemChanged" >
                    </TreeView>
                </Grid>
            </GroupBox>

            <StackPanel Orientation="Vertical" Grid.Column="1">
                <GroupBox Grid.Row="1" Margin="5" Header="3. Choose a period">
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock>From</TextBlock>
                        <DatePicker SelectedDate="{Binding Path=DateFrom, Mode=OneWayToSource}" />
                        <TextBlock>To</TextBlock>
                        <DatePicker SelectedDate="{Binding Path=DateTo, Mode=OneWayToSource}" />
                    </StackPanel>
                </GroupBox>

                <GroupBox  Header="4. Load changesets">
                    <Grid Margin="5">
                        <Button Content=" Load changesets for period " Command="{Binding CmdLoadChangeSets}" />
                    </Grid>
                </GroupBox>

                <GroupBox Header="5. Choose the changeset scope">
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Text="From changeset" />
                        <ComboBox ItemsSource="{Binding Source={StaticResource ChangesetsFromLimit}}" MinWidth="175" ItemTemplate="{StaticResource ChangesetDataTemplate}" SelectedItem="{Binding Path=ChangeSetFrom, Mode=OneWayToSource}">
                            <ComboBox.IsEnabled>
                                <Binding Path="ChangeSets" Converter="{StaticResource ItemsExistsToBoolEnabledConverter}" Mode="OneWay"></Binding>
                            </ComboBox.IsEnabled>
                        </ComboBox>
                        <TextBlock Text="to changeset" />
                        <ComboBox ItemsSource="{Binding Source={StaticResource ChangesetsToLimit}}" MinWidth="175" ItemTemplate="{StaticResource ChangesetDataTemplate}" SelectedItem="{Binding Path=ChangeSetTo, Mode=OneWayToSource}" >
                            <ComboBox.IsEnabled>
                                <Binding Path="ChangeSets" Converter="{StaticResource ItemsExistsToBoolEnabledConverter}" Mode="OneWay"></Binding>
                            </ComboBox.IsEnabled>
                        </ComboBox>
                    </StackPanel>
                </GroupBox>

                <GroupBox Header="6. Load Results">
                    <Grid Margin="5">
                        <Button Content=" Load artifacts " Command="{Binding CmdLoadArtifacts}" />
                    </Grid>
                </GroupBox>
            </StackPanel>
        </Grid>

        <GroupBox Grid.Row="0" Header="1. Team Project">
            <StackPanel Orientation="Horizontal" Margin="5">
                <Button Content=" Choose a team project " Command="{Binding CmdChangeSource}" />
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="Connected on project collection {0} ({1}) - Project : {2}" Mode="OneWay" >
                            <Binding Path="ProjectCollectionName" Mode="OneWay" />
                            <Binding Path="ProjectCollectionUri" Mode="OneWay" />
                            <Binding Path="ProjectName" Mode="OneWay" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </StackPanel>
        </GroupBox>

        <GroupBox Header="Actions on workitems found" Grid.Row="2">
            <ItemsControl  ItemsSource="{Binding ExternalActions}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Command="{Binding Path=DataContext.CmdExternalAction, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" CommandParameter="{Binding}"  ToolTip="{Binding TooltipDescription}">
                            <Button.Content>
                                <Grid Width="auto" Height="auto">
                                    <TextBlock Margin="5,2" Text="{Binding Path=DisplayName}" />
                                </Grid>
                            </Button.Content>
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl >
        </GroupBox>

        <GroupBox Grid.Row="3">
            <GroupBox.Header>
                <TextBlock>
                    <TextBlock.Text>
                        <Binding Path="LinkedArtifacts.Count" StringFormat=" {0} item(s) found" Mode="OneWay"  FallbackValue="0" TargetNullValue="0" ></Binding>
                    </TextBlock.Text>
                </TextBlock>
            </GroupBox.Header>
            <DataGrid ItemsSource="{Binding Path=LinkedArtifacts}" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=Id}" Header="Id" />
                    <DataGridTextColumn Binding="{Binding Path=Type.Name}" Header="Workitem type" />
                    <DataGridTextColumn Binding="{Binding Path=State}" Header="State" />
                    <DataGridTextColumn Binding="{Binding Path=Title}" Header="Title" />
                </DataGrid.Columns>
            </DataGrid>
        </GroupBox>
    </Grid>
</Window>
